package cn.jpush.api.examples;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javacommon.util.DateTimeUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.xml.internal.bind.v2.runtime.output.Encoded;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.DeviceEnum;
import cn.jpush.api.push.CustomMessageParams;
import cn.jpush.api.push.MessageResult;
import cn.jpush.api.push.ReceiverTypeEnum;
import cn.jpush.api.report.ReceivedsResult;


public class JPushClientExample {
    protected static final Logger LOG = LoggerFactory.getLogger(JPushClientExample.class);

    // demo App defined in resources/jpush-api.conf 
	private static final String appKey ="58edc5ae8ae52636223b6df7";
	private static final String masterSecret = "547f4c9f7b99f5af3d95cbe5";
	private static final String appKey1 ="42707b556e5a98d2e752cd1d";
	private static final String masterSecret1 = "8987c7c39d9cd1edd48126a4";
	
	public static final String msgTitle = "Test from API example";
    public static final String msgContent = "Test Test";
    public static final String registrationID = "0900e8d85ef";
//    public static final String tag = "f39d2d2a43ae8e790143b4e9c3fc1b25";

	public static void main(String[] args) {
//		testSend();
//		testGetReport();
//		char a1='1',a2='点',a3='进',a4='攻';
//		char secret='密';
//		
//		a1 = (char)(a1 ^ secret);
//		a2 = (char)(a2 ^ secret);
//		a3 = (char)(a3 ^ secret);
//		a4 = (char)(a4 ^ secret);
//		String secretStr = "密文："+a1+a2+a3+a4;
//		a1 = (char)(a1 ^ secret);
//		a2 = (char)(a2 ^ secret);
//		a3 = (char)(a3 ^ secret);
//		a4 = (char)(a4 ^ secret);
//		String realStr = "明文：" + a1 + a2 + a3 + a4;
//	//	System.out.print((int)secret);
//		String str="0123456789abcdefghijklmnopqrstuvwxyz";
//		byte b=0x11;
//		try {
//			byte[] bytes=str.getBytes("UTF-8");
//			for(int i=0;i<bytes.length;i++){
//				bytes[i]=(byte) (bytes[i]^b);
//			}
//			String str3=new String(bytes);
//			bytes=str3.getBytes();
//	//	bytes=str1.getBytes();
//			for(int i=0;i<bytes.length;i++){
//				bytes[i]=(byte) (bytes[i]^b);
//			}
//			String str2=new String(bytes);
//			System.out.print(new String(bytes));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//send("f39d2d2a44ef04790144ef78736b0727","任务挖财",244,0,0);
//		Date nowDate = DateTimeUtil.toDate("14-03-31 23:53:19");
//		Date crDate = DateTimeUtil.toDate("14-03-31 23:52:14");
//	
//		double differ = DateTimeUtil.compareSecond(nowDate,
//				crDate);
//		System.out.print(differ);
	}
	private static long timeToLive =  60 * 60 * 24; 
	public static void send(String tag,String appName,int point,int app,int isErr) {//0表示机赚，1表示先生 ，
		JPushClient jpushClient = null;
		if(app==0)
        jpushClient = new JPushClient(masterSecret, appKey, timeToLive, DeviceEnum.IOS, true);
		else 
			 jpushClient = new JPushClient(masterSecret1, appKey1, timeToLive, DeviceEnum.IOS, true);
		MessageResult msgResult=null;
		if(isErr==0)
		 msgResult = jpushClient.sendNotificationAll("恭喜你完成"+appName+"获得了"+point+"积分",tag);//格式为     恭喜你完成appname获得了XXX积分
		else if(isErr==3)
			 msgResult = jpushClient.sendNotificationAll("对不起，为了保证广告商的推广效果，同一个网络下仅允许一台设备，因您的网络下设备过多，本次任务无效，请换个网络再做，谢谢",tag);//格式为     恭喜你完成appname获得了XXX积分
		else if(isErr==2)
			 msgResult = jpushClient.sendNotificationAll("亲，上个任务做完后→回机赚→点广告→跳转到appstore→下载→安装→再打开，此过程要好几分钟的呢，四核+100M光纤也没你快啊，请不要投机取巧，否则会永久拉黑的哟",tag);//格式为     恭喜你完成appname获得了XXX积分
        LOG.debug("responseContent - " + msgResult.responseResult.responseContent);
		if (msgResult.isResultOK()) {
	        LOG.info("msgResult - " + msgResult);
	        LOG.info("messageId - " + msgResult.getMessageId());
		} else {
		    if (msgResult.getErrorCode() > 0) {
		        // 业务异常
		        LOG.warn("Service error - ErrorCode: "
		                + msgResult.getErrorCode() + ", ErrorMessage: "
		                + msgResult.getErrorMessage());
		    } else {
		        // 未到达 JPush 
		        LOG.error("Other excepitons - "
		                + msgResult.responseResult.exceptionString);
		    }
		}
	}
	
	public static void testGetReport() {
        JPushClient jpushClient = new JPushClient(masterSecret, appKey);
		ReceivedsResult receivedsResult = jpushClient.getReportReceiveds("1708010723,1774452771");
        LOG.debug("responseContent - " + receivedsResult.responseResult.responseContent);
		if (receivedsResult.isResultOK()) {
		    LOG.info("Receiveds - " + receivedsResult);
		} else {
            if (receivedsResult.getErrorCode() > 0) {
                // 业务异常
                LOG.warn("Service error - ErrorCode: "
                        + receivedsResult.getErrorCode() + ", ErrorMessage: "
                        + receivedsResult.getErrorMessage());
            } else {
                // 未到达 JPush
                LOG.error("Other excepitons - "
                        + receivedsResult.responseResult.exceptionString);
            }
		}
	}

}

